function select_juan() {
	var line_member_id = $("#line_member_id").val().trim();
	var price = $("#price").val();
	if(line_member_id == "") {
		layer.msg("请先选择会员!");
		return false;
	}
	if(price == "" || isNaN(price)) {
		layer.msg("请输入正确的账单金额!");
		return false;
	}
	layer.open({
		type: 2,
		title: '选择优惠券',
		id: "select_juan",
		area: ['650px', '450px'],
		scrollbar: false,
		content: '/web/cashier/select_juan?member_id=' + line_member_id
	});
}

function change_score(obj) {
	if($(obj).is(":checked")) {
		var lime_member_id = $("#line_member_id").val();
		if(lime_member_id == "") {
			layer.msg("请选择会员后再选择是否送积分");
			$(obj).attr("checked", false);
			return;
		}
		$("#zs_score").val($("#ys_money").val());
	} else {
		$("#zs_score").val(0);
	}

}

function change_ftscore(obj) {
	if($(obj).is(":checked")) {
		var lime_member_id = $("#line_member_id").val();
		if(lime_member_id == "") {
			layer.msg("请选择会员后再选择是否送积分");
			$(obj).attr("checked", false);
			return;
		}
		$("#zs_score").val($("#zhe_price").val());
	} else {
		$("#zs_score").val(0);
	}

}

function keyup_price(obj) {
	obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
	obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
	obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
	obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	document.getElementById('ys_money').value = $(obj).val();
	var zs_score=$("#zs_score");
	var checked=zs_score.parent().find("span").find("input[type='checkbox']");
	if(checked.is(":checked")){
		document.getElementById('zs_score').value = $(obj).val();
	}
	$("#pay_method").val("");
	$("#pay_money").val("");
	$(".sku").removeClass('sku-bo-blo'); //   清空勾选图标
	$(".sku").find("s").css("display", "none");
}
function clear_price(obj) {
	obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
	obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
	obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
	obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}
function keyup_pay(obj) {
	var ys = $("#ys_money").val();
	if(!ys){
		ys = 0;
	}
	var pay =$("#pay_money").val();
	if(!pay){
		pay = 0;
	}
	var zl = parseFloat(pay) - parseFloat(ys);
	$("#zhaoling").text(zl.toFixed(2));
}
$("#key").autocomplete({
	url: 'get_emps',
	data: {
		key: $("#key").val()
	},
	css: {
		'width': '88%',
		'border': '0',
	},
	postname: 'id'
});

function switch_spec(spec) {
	var pay_method = $(spec).attr("data");
	if(pay_method == 3 || pay_method == 4 || pay_method == 2||pay_method==7) {
		$("#pay_money").val($("#ys_money").val());
		$("#zhaoling").text(0);
	}
	$("#pay_method").val(pay_method);
	//      $(spec).siblings('input').trigger('click');	 // 让隐藏的 单选按钮选中
	$(spec).parent().parent().find(".sku").removeClass('sku-bo-blo'); //   清空勾选图标
	$(spec).parent().parent().find(".sku").find("s").css("display", "none");
	$(spec).addClass('sku-bo-blo'); // 当前 加上勾选图标
	$(spec).find("s").css('display', "block"); //   
}

function adduser() {
	layer.open({
		type: 2,
		id: "add_member",
		area: ['1050px', '550px'],
		scrollbar: false,
		content: '/web/cashier/add_member' //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
	});
}
//清空
function empty() {
	$("#pay_method").val("");
	$("#pay_money").val("");
	$("#zhaoling").text("");
	$(".sku").removeClass('sku-bo-blo'); //   清空勾选图标
	$(".sku").find("s").css("display", "none");
	$("#price").val("");
	$("#zhe_price").val("");
	$("#zs_score").val("");
	$("#ys_money").val("");
	$("#key").val("");
	$("#member_info").hide();
	$('#menu_bottom').show();
	$('#goods_cart').html("");
	$("#emp_info").html("");
	cartlist = {};
	$("#total_price").text("0");
	$("[name=items]:checkbox").attr("checked", false);
	$("#cz_price").val("");
}
function del_emp(){
	$("#emp_info").html("");
	$('#dg_empno').val('');
}
function search_emp() {
	var loading = layer.load(0, {
		shade: false,
		time: 3 * 1000
	});
	$.ajax({
		url: 'emplist',
		data: {},
		type: "post",
		success: function(data) {
			layer.close(loading);
			ems = data.data;
			console.log(ems);
			var res = '<div class="example-wrap"><div class="example">' +
				'<table class="table table-bordered table-hover">' +
				'<thead><tr class="long-tr"><th></th><th>工号</th><th>姓名</th> <th>手机号</th><th>店铺名称</th></tr>  </thead>' +
				'<tbody id="emplist">';
			for(var i = 0; i < ems.length; i++) {
				var emo = ems[i];
				var emoname = data.empname;
				if(emo.chainname){
					emoname = emo.chainname;
				}
				res += '<tr>' +
					'<td ><input name="dg" data-id="' + emo.id + '" data-mobile="' + emo.mobile + '" data-no="' + emo.no + '" value="' + emo.id + '" type="checkbox"/></td><td>' + emo.no + '</td><td>' + emo.name + '</td><td>' + emo.mobile+ '</td><td>' + emoname + '</td>' +
					'</tr>';
			}
			res += '</tbody></table></div></div>';
			var html = '<div class="form-group" id="add_form">' +
				'<div class="input-group col-sm-6">' +
				'<span id="error_tips" style="color: red;display: none;">请选择导购员</span></div></div>' + res;
			var index = layer.open({
				title: '选择导购员',
				zIndex: 1999,
				area: ['700px', '480px'],
				content: html,
				btn: ['确定'],
				yes: function(index, layero) {
					var emp_ = $("#emp_info");
					$('input[name="dg"]:checked').each(function(){
						var dg = $(this).attr("data-id");
						var no = $(this).attr("data-no");
						var mobile = $(this).attr("data-mobile");
						var info = '<tr><td id="zno">' + no + '</td><td >' + mobile + '</td><td style="margin-left:30px;"><input data-id="'+dg+'" onkeyup="clear_price(this)" name="dg_money" class="shouy_tllinp" type="text" value=0 style="text-align:center;height: 35px;line-height: 35px;border: 1px;"/></td></tr>';
						emp_.append(info);
						
					}); 
						
						layer.close(index);
					}
			});

		}
	})

}
/**
 * 
 * @param {Object} type 1快速2商品3房台
 */
function confirm_sk(type,name,mobile) {
	var price = parseFloat($("#ys_money").val());
	if(price == "" || isNaN(price)) {
		layer.msg("请输入正确的账单金额!");
		return false;
	}
	var pay_method = $("#pay_method").val();
	if(pay_method == "") {
		layer.msg("请选择支付方式!");
		return false;
	}
	var pay_money = parseFloat($("#pay_money").val());
	if(pay_money == "" || isNaN(pay_money)) {
		layer.msg("请输入正确的付款金额!");
		return false;
	}
	if(pay_money < price) {
		layer.msg("付款金额小于账单金额!");
		return false;
	}
	if(pay_method == 2) {
		if($("#key").val() == "") {
			layer.msg("请输入正确的会员卡号!");
			return false;
		}
		var yue = parseFloat($("#money").text());
		if(yue < parseFloat(price)) {
			layer.msg("余额不足!");
			return false;
		}
	}
	
	
	var dg =new Array();
	var dg_money=new Array();
	$('input[name="dg_money"]').each(function(){
		dg.push($(this).data('id'));
		dg_money.push($(this).val());
	}); 
	
	var zhaoling = parseFloat($("#zhaoling").text());
	var data = {
		dg_money: dg_money,
		type: type,
		zs_score: $("#zs_score").val(),
		price: price,
		pay_money: pay_money,
		zhaoling: zhaoling,
		pay_method: pay_method,
		cz_price: $("#cz_price").val(),
		dg_id: dg,
		line_member_id: $("#line_member_id").val(),
		memo: $("#ccomment").val()
	};
	console.log(data);
	var action='shoukuan';
	if(type == 1) {} else if(type == 2) { //商品
		data['data'] = cartlist;
	} else if(type == 3) { //扫码
		data['data'] = cartlist;
	} else if(type == 4) { //房台
		data['orderId'] = $("#orderId").val();
		data['house_id'] = $(".room-selected").attr("data-id");
	} else if(type==5){//冲次
		if($("#key").val()==""){
			layer.msg("请选择会员!");
			return false;
		}
		data['data'] = cartlist;
	} else if(type ==6){//充值
		if($("#key").val()==""){
			layer.msg("请选择会员!");
			return false;
		}
		action="chongzhi";
	}
	
	var sjprice = parseFloat($("#money").text());
	if(type ==6){
		sjprice = parseFloat($("#money").text()+parseFloat($("#cz_price").val()));
	}else{
		if(pay_method==2){
			sjprice = parseFloat($("#money").text()-parseFloat($("#ys_money").val()));
		}		
	}
	
	
	var i=0;
	var html = '<div class="row" >' +
		'<div style="float: left;width:50%;text-align: center;">' +
		'<span id="sp_price" style="font-size:26px;font-weight:600;color:#35b12c">' + price + '</span>' +
		'<p>应收金额</p>' +
		'</div>' +
		'<div style="float: left;width:50%;text-align: center;">' +
		'<span id="sp_zhaoling" style="font-size:26px;font-weight:600;color:red">' + zhaoling + '</span>' +
		'<p>找零</p>' +
		'</div>' +
		'</div>';
		if(type==6||type==5){
				html+='<div class="row" >'+
						'<div style="float: left;width:50%;text-align: center;">'+
							'<span>会员卡号</span>&nbsp<label>'+$("#no").text()+'</label>'+
						'</div>'+
						'<div style="float: left;width:50%;text-align: center;">'+
							'<span>消费后余额</span>&nbsp<label>'+ sjprice +'</label>'+
						'</div>'+
				   '</div>'+
					'<div class="row" >'+
						'<div style="float: left;width:50%;text-align: center;">'+
							'<span>手机号</span>&nbsp<label>'+mobile+'</label>'+
						'</div>'+
						'<div style="float: left;width:50%;text-align: center;">'+
							'<span>消费后积分</span>&nbsp<label>'+(parseFloat($("#score").text())+parseFloat($("#zs_score").val()))+'</label>'+
						'</div>'+
				   '</div>';	
		}
		html+='<div class="row" >' +
		'<div style="float: left;width:50%;text-align: center;">' +
		'<span>门店</span>&nbsp<label>'+name+'</label>' +
		'</div>' +
		'<div style="float: left;width:50%;text-align: center;">' +
		'<span>收银员</span>&nbsp<label>'+mobile+'</label>' +
		'</div>' +
		'</div>';
	var index = layer.open({
		title: '确认收款',
		zIndex: 1999,
		area: ['500px', '250px'],
		content: html,
		btn: ['确定', '取消'],
		yes: function(index, layero) {
			if(i==1){
				return;
			}
			i=1;
			$.ajax({
				url: action,
				data: data,
				type: "post",
				success: function(dataa) {
					layer.close(index);
					if(dataa.code!=200){
						layer.msg(dataa.msg);
						return;
					}
					if(pay_method ==7) {
						var dt = dataa.data;
						var code_url=dt.code_url;
						var store_logo = dt.store_logo;
						var ct = '<div id="qrcode" style="margin-left:100px;padding:0;"><div>';
						var index1 = layer.open({
							area: ['450px', '350px'],
							zIndex: 1999,
							title: "扫码支付",
							content: ct,
							btn: ['支付完成', '取消'],
							yes: function(index, layero) {
								$.ajax({
									type: "post",
									url: "confirm_pay",
									data: {
										sn: dt.sn
									},
									success: function(data) {
										if(data == 1) {
											callback(dataa, type);
										} else {
											layer.msg("订单支付失败");
										}
									}
								});
							},
							btn2: function(index, layero) {
								layer.close(index1);
							}
						});
						$("#qrcode").qrcode({
			             render : "canvas",    //设置渲染方式，有table和canvas，使用canvas方式渲染性能相对来说比较好
			             text : code_url,    //扫描二维码后显示的内容,可以直接填一个网址，扫描二维码后自动跳向该链接
			             width : "200",               //二维码的宽度
			             height : "200",              //二维码的高度
			             background : "#ffffff",       //二维码的后景色
			             foreground : "#000000",        //二维码的前景色
			             src: store_logo             //二维码中间的图片
   					 	});
					} else {
						callback(dataa,type);
					}

				}
			})
		},
		btn2: function(index, layero) {
			layer.close(index);
		}
	});

}
function call_back(msg){
		layer.msg(msg);
		layer.closeAll('iframe');
	}
function callback(data, type) {
	if(data.code == 200) {
		var info = data.data;
		var html = '<div>' +
			'<div style="text-align: center;">' +
			'<i class="fa fa-check" style="color: #35b12c;font-size: 60px;font-weight: 600;"></i>' +
			'<p><span class="b_rs_pan">收款成功</span> </p>' +
			'</div>' +
			'<div class="form-group" style="padding-left: 100px;"><label class="col-sm-5 control-label">单号：</label><div class="input-group col-sm-7">' + info.sn + '</div></div>' +
			'<div class="form-group" style="padding-left: 100px;"><label class="col-sm-5 control-label">交易时间：</label><div class="input-group col-sm-7">' + info.add_time + '</div></div>' +
			'<div class="form-group" style="padding-left: 100px;"><label class="col-sm-5 control-label">门店：</label><div class="input-group col-sm-7">' + info.store_name + '</div></div>' +
			'<div class="form-group" style="padding-left: 100px;"><label class="col-sm-5 control-label">操作员：</label><div class="input-group col-sm-7">' + info.skr + '</div></div>' +
			'</div>';
		var index = layer.open({
			title: '收款成功',
			zIndex: 1999,
			area: ['600px', '400px'],
			content: html,
			btn: ['我知道了', '打印小票'],
			yes: function(index, layero) {
				do_back(type);
				layer.close(index);				
			},
			btn2: function(index, layero) {
				$.ajax({
					type: "POST",
					url: "print_order",
					data: {
						sn: data.data.sn
					},
					success: function(dt) {}
				});
				layer.close(index);
				do_back(type);
			}
		});
	} else {
		layer.msg(data.msg);
	}
}

function do_back(type) {
	empty();
	if(type != 1&&type!=6) {
		$("#container").show();
		$("#container1").hide();
	}
	if(type == 2) { //商品消费
		reset_gd();
	} else if(type == 3) { //扫码
		$("#car_box").show();
		reset_gd();
	} else if(type == 4) { //房台
		$("#container2").hide();
		$("#goods_cart1").html("");
		reset_gd();
	}else if(type == 5) { //充次
		reset_gd();
	}
}